package encaps.utils;

import encaps.action.Action;
import encaps.action.ActionExecutionException;
import encaps.resources.Labels;
import encaps.security.SecuritySystem;
import org.jdom.Element;

public class ActionExecutor {

    private ActionExecutor() throws InitializationException {
    }

    public static Element executeAction(String userRole, Action action) throws ActionExecutionException {
        try {
            if (action == null) {
                return null;
            }
            if (SecuritySystem.isActionAllowed(userRole, action)) {
                return action.execute();
            } else {
                Element e = XMLHelper.createTextMessage(Labels.getLabel(Labels.HAVE_NO_PERMISSIONS_MESSAGE));
                return e;
            }
        } catch (Exception ex) {
            Element e = XMLHelper.createTextMessage(Labels.getLabel(Labels.SYSTEM_INTERNAL_ERROR) + " Message: " + ex.getMessage());
            return e;
        }
    }
}
